//
//  HistoricalOrderController.m
//  ios-supplier
//
//  Created by Benlai on 15/7/31.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "HistoricalOrderController.h"
#import "LZSegmentControl.h"
#import "Masonry.h"
#import "iCarousel.h"
#import "HistoricalOrderListView.h"


@interface HistoricalOrderController ()<UIGestureRecognizerDelegate,iCarouselDataSource,iCarouselDelegate>
@property (nonatomic,assign) NSInteger currentType;
@property (nonatomic, strong) UITableView *historialOrderTable;

@property (nonatomic, strong) NSMutableArray *orderArray;

@property (nonatomic, assign) NSInteger pager;
@property (nonatomic,strong) LZSegmentControl *mySegmentControl;
@property (nonatomic,strong) iCarousel *myCarousel;

@end

@implementation HistoricalOrderController

- (void) setUI{
    
    __weak typeof(self) weakSelf = self;
    CGRect segmentFrame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
    _mySegmentControl = [[LZSegmentControl alloc] initWithFrame:segmentFrame Items:@[@"全部",@"待审核",@"待发货",@"已发货",@"已完成",@"已拒绝"]];
    [self.view addSubview:_mySegmentControl];
    [_mySegmentControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.view);
        make.top.equalTo(@64);
        make.height.equalTo(@40);
    }];

    _myCarousel = [[iCarousel alloc] init];
    _myCarousel.dataSource = self;
    _myCarousel.delegate = self;
    _myCarousel.decelerationRate = 1.0;
    _myCarousel.scrollSpeed = 1.0;
    _myCarousel.type = iCarouselTypeLinear;
    _myCarousel.pagingEnabled = YES;
    _myCarousel.clipsToBounds = YES;
    _myCarousel.bounces = NO;
    [self.view addSubview:_myCarousel];
    [_myCarousel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(_mySegmentControl.mas_bottom);
    }];
    //_myCarousel.backgroundColor = [UIColor yellowColor];
    _mySegmentControl.valueChange = ^(NSInteger index){
        [weakSelf.myCarousel scrollToItemAtIndex:index animated:NO];
        
        NSLog(@"valurChange-----%ld",index);
    };
    
    
    //    currentFilter = 0;
    //
    //    self.orderArray = [[NSMutableArray alloc]init];
    //
    //    self.pager = 1;
    
    //    self.historialOrderTable = [[UITableView alloc]initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style: UITableViewStylePlain];
    //    [self.historialOrderTable registerNib: [UINib nibWithNibName: @"HistoryOrderCell" bundle: nil] forCellReuseIdentifier: @"historyOrderCell"];
    //    self.historialOrderTable.delegate = self;
    //    self.historialOrderTable.dataSource = self;
    //    [self.historialOrderTable setSeparatorStyle: UITableViewCellSeparatorStyleNone];
    //    self.historialOrderTable.backgroundColor = [UIColor colorFromHexValue: 0xeeeeee];
    //
    //    [self.view addSubview: self.historialOrderTable];
    
    //    [self reloadData];
    
    //    [self.historialOrderTable addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(headerLoadNewData)];
    //
    //    [self.historialOrderTable addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(footerLoadNewData)];
    //
    //    [self.historialOrderTable.footer setTitle: @"没有更多历史订单了" forState: MJRefreshFooterStateNoMoreData];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    
    [self.navigationItem setLeftBarButtonItem: [self createBackView]];
    [self showWithType:self.currentType];

}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.currentType = 0;
    [self setUI];
}
- (void) showWithType:(NSInteger)type{
   self.currentType = type;
    _mySegmentControl.selectSegmentIndex = self.currentType;
}
#pragma mark iCarousel M
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel{
    return 6;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view{
     HistoricalOrderListView *listView = [[HistoricalOrderListView alloc]initWithFrame:carousel.bounds type:index viewController:self];
    
    return listView ;
}


- (void)carouselDidScroll:(iCarousel *)carousel{
    float offset = carousel.scrollOffset;
    [self.mySegmentControl moveIndexWithProgress:offset];
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel{
    if (carousel.currentItemIndex == 1) {
        return;
    }
    [(HistoricalOrderListView*)carousel.currentItemView loadDataForWebWithType:carousel.currentItemIndex];
}

- (UIBarButtonItem *)createBackView
{
    UIButton *leftButtonItem = [UIButton buttonWithType:UIButtonTypeCustom];
    
    leftButtonItem.frame = CGRectMake(0, 0, 11, 20);
    
    [leftButtonItem setBackgroundImage:[UIImage imageNamed:@"icon-Back"] forState:UIControlStateNormal];
    
    [leftButtonItem setImageEdgeInsets: UIEdgeInsetsMake((19 - 11) / 2, 0, (19 - 11) / 2, 0)];
    
    [leftButtonItem addTarget:self action:@selector(leftButtonItemClicked) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:leftButtonItem];
    
    return item;
    
}

-(void)leftButtonItemClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)viewWillAppear:(BOOL)animated
{
    if (self.orderArray.count == 0)
    {
        [self.historialOrderTable.footer setTitle: @"" forState: MJRefreshFooterStateIdle];
    }
    else
    {
        [self.historialOrderTable.footer setTitle: @"点击加载更多" forState: MJRefreshFooterStateIdle];
    }
}

-(void)headerLoadNewData
{
//    [JFBSubmitOrderDS loadMenuList:^(NSArray *menuList, NSError *error) {
//
//        self.orderArray = [NSMutableArray arrayWithArray: menuList];
//        
//        [self.historialOrderTable reloadData];
//        [self.historialOrderTable.header endRefreshing];
//        
//    } page: 1];
}

-(void)footerLoadNewData
{
//    [JFBSubmitOrderDS loadMenuList:^(NSArray *menuList, NSError *error) {
//
//        if (menuList.count == 0)
//        {
//            [self.historialOrderTable.footer noticeNoMoreData];
//            return ;
//        }
//        [self.orderArray addObjectsFromArray:[NSMutableArray arrayWithArray: menuList]];
//        [self.historialOrderTable reloadData];
//
//        [self.historialOrderTable.footer endRefreshing];
//
//    } page:self.pager++];
}


//-(void)reloadData
//{
//    [self.orderArray removeAllObjects];
//    JFBLoadingAnimation *animation = [[JFBLoadingAnimation alloc] init: self.view];
//    
//    [JFBSubmitOrderDS loadMenuList:^(NSArray *menuList, NSError *error) {
//        [animation stop];
//        [self.orderArray addObjectsFromArray: menuList];
//        [self.historialOrderTable reloadData];
//        
//    }andWithUT:[UserInfo currentUser].ut andWithMerchantId:[[UserInfo currentUser].merchantID integerValue] andWithPurchaseStatus:self.currentType andWithCurrentPage:1 andWithItemsPerPage:10 andWithOrderItemNum:0];
//}

//#pragma mark - 数据源方法
//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return self.orderArray.count;
//}
//
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 110;
//}

//
//-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *identifier = @"historyOrderCell";
//    HistoryOrderCell *cell = [tableView dequeueReusableCellWithIdentifier: identifier];
//
//    cell.historyDS = self.orderArray[indexPath.row];
//    
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    return cell;
//}
//
//
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName: @"StoreManage" bundle: nil];
//    OrderGoodsDetailController *orderDetailC = (OrderGoodsDetailController *)[storyBoard instantiateViewControllerWithIdentifier: @"orderGoodsDetailController"];
//    JFBSubmitOrderDS *detail = self.orderArray[indexPath.row];
//    orderDetailC.orderDetailInfo = detail;
//    
//    [self.navigationController pushViewController: orderDetailC animated: YES];
//
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
